package edt;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import org.joda.time.LocalTime;

public class AddDialogListener implements ActionListener{

	MySchedulerModel model;
	AddDialog v;
	
	
	public AddDialogListener(AddDialog laddDialog, MySchedulerModel theModel) {
		v= laddDialog;
		model = theModel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		
				
		String type = "";
		/* recuperation du type de cours */
	    for (Enumeration<AbstractButton> ee = v.bg.getElements(); ee.hasMoreElements();) {
	        JRadioButton b = (JRadioButton) ee.nextElement();
	        if (b.getModel() == v.bg.getSelection()) {
	        	type = b.getText();
	        }
	    }
		
	    
		LocalTime debut = new LocalTime(0,0,0);
		try {
			
			debut = new LocalTime( Integer.parseInt(v.debut.getValue().toString()),0,0);
		
			CreneauModele appointment = new CreneauModele(v.matiere.getText(), debut);
			
			appointment.setType(type);
			
			appointment.setDuration(Integer.parseInt(v.duree.getValue().toString()));
			
			appointment.setGroupe(v.groupe.getText());
			
			appointment.setClasse(v.classe.getText());
			
			if (v.groupe.getText().isEmpty() || v.classe.getText().isEmpty()) {
				throw new Exception("erreur de saisie");
			}
			
			
			
			
			if( v.date.getSelectedItem().equals("Lundi")) {
				appointment.set_resource(model.getLundi());
			}
			else if(v.date.getSelectedItem().equals("Mardi")) {
				appointment.set_resource(model.getMardi());
			}
			else if(v.date.getSelectedItem().equals("Mercredi")) {
				appointment.set_resource(model.getMercredi());
			}
			else if(v.date.getSelectedItem().equals("Jeudi")) {
				appointment.set_resource(model.getJeudi());
			}
			else if(v.date.getSelectedItem().equals("Vendredi")) {
				appointment.set_resource(model.getVendredi());
			}

			
			model.addAppointment(appointment);
			appointment.setAlreadySync(false);
		
		
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(v, "Erreur de saisie", "Erreur",
			        JOptionPane.ERROR_MESSAGE);		
	    } catch (Exception e1) {
			JOptionPane.showMessageDialog(v, "Erreur de saisie", "Erreur",
			        JOptionPane.ERROR_MESSAGE);	
		}
		
	}

}
